using System;
using System.Collections.Generic;
using System.Text;
using AntEater.ComponentModel.BuildElements;
using System.Xml.XPath;
using System.Xml;
using AntEater.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace AntEater.Runtime.Services.Transformation
{

    /// <summary>
    /// Provides a transfomer for the OnErrorBuildElement.
    /// </summary>
    [BuildElementTransformer(typeof(OnErrorBuildElement), XPathNodeType.Element)]
    internal class OnErrorTransformer:BuildElementTransformerBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:OnErrorTransformer"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public OnErrorTransformer(object context)
            : base(context) {
        }

        /// <summary>
        /// Transforms an <c>BuildElement</c> into XML.
        /// </summary>
        /// <param name="buildElement">The build element.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="errors">The errors.</param>
        public override void GetXml(IBuildElement buildElement, XmlWriter writer, List<TransformationError> errors) {
            OnErrorBuildElement element = buildElement as OnErrorBuildElement;

            if (element != null) {

                BuildElementAttribute buildElementAttribute
                    = ComponentModel.Utilities.GetTypeAttributeFromObject<BuildElementAttribute>(buildElement);

                if (buildElementAttribute != null) {
                    writer.WriteStartElement(buildElementAttribute.Name);

                    string executeTargets = BuildElementPropertyTransformationHelper.ListOfTargetsToXml(element.ExecuteTargets);
                    if (!string.IsNullOrEmpty(executeTargets)) {
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("ExecuteTargets"), executeTargets);
                    }
                    else {
                        errors.Add(new TransformationError("ExecuteTargets attribute isn't specified!", element));
                    }

                    if (!string.IsNullOrEmpty(element.Condition)) {
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("Condition"), element.Condition);
                    }

                    this.OnGetChildElementsXml(element, writer);

                    writer.WriteEndElement();
                }
            }
        }

        /// <summary>
        /// Gets an <c>BuildElement</c> from XML.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="buildFileDefinition">The build file definition.</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public override IBuildElement GetBuildElement(XPathNavigator node, BuildFileDefinition buildFileDefinition, List<TransformationError> errors) {
            OnErrorBuildElement element = buildFileDefinition.CreateNewBuildElement(typeof(OnErrorBuildElement)) as OnErrorBuildElement;

            bool executeTargetsFound = false;

            if (node.MoveToFirstAttribute()) {
                do {
                    if (node.Name.Equals(this.GetBuildElementPropertyName("ExecuteTargets"))) {
                        TargetCollection executeTargets = BuildElementPropertyTransformationHelper.ListOfTargetsFromXml(node.Value, buildFileDefinition);
                        if (executeTargets != null) {
                            if (executeTargets.Count > 0) {
                                element.ExecuteTargets.AddRange(executeTargets);
                                executeTargetsFound = true;
                            }
                        }
                    }
                    else if (node.Name.Equals(this.GetBuildElementPropertyName("Condition"))) {
                        element.Condition = node.Value;
                    }
                } while (node.MoveToNextAttribute());
            }

            if (!executeTargetsFound) {
                errors.Add(new TransformationError("ExecuteTargets attribute is missing!", element));
            }

            return element;
        }
    }
}
